package com.MarkToTop.main;

import com.MarkToTop.util.Constant;
import com.MarkToTop.util.GameUtil;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * MarkWang 版权所有 © Copyright 2020
 *
 * @CreateDate: Created in 2020-06-21 08:44
 * @Author: MarkToTop -(づ｡◕‿‿◕｡)づ
 * @Description:   游戏中障碍物层的类，生成、管理障碍物层(障碍物容器);
 */
public class GameBarrierLayer {

    //障碍物出现规则
    public static final int TYPE1_VER_INTERVAL = Constant.Frame_Height >> 2;
    public static final int TYPE1_HOR_INTERVAL = Constant.Frame_Height >> 2;
    public static final int TYPE1_MIN_HEIGHT = Constant.Frame_Height >> 3;
    public static final int TYPE1_MAX_HEIGHT = (Constant.Frame_Height >> 3)*5;

    private List<Barrier> barries;

    public GameBarrierLayer() {
        barries = new ArrayList<>();

    }


    public void draw(Graphics g,Bird bird){
        for (int i = 0; i < barries.size(); i++) {
            //如果可见true就绘制，不可见false就归还
            Barrier barrier = barries.get(i);
            if(barrier.isVisible()){
                barrier.draw(g,bird);
            }else {
                Barrier remove = barries.remove(i);
                BarrierPooL.giveBack(remove);
                i--;
            }
        }
        //碰撞检测
        BirdCrush(bird);
        login(bird);
    }

    /**
     * 添加障碍物的逻辑
     * 当容器中添加完最后一个障碍物，完全显示后，就继续添加下一对
     */
    private void login(Bird bird){
        //鸟死亡后不再添加新的障碍物
        if (bird.isDie()) return;
        if(barries.size() == 0){
            //游戏刚开始
            int topHeight = GameUtil.getRandomNumber(TYPE1_MIN_HEIGHT,TYPE1_MAX_HEIGHT+1);
            int type = Barrier.TYPE_TOP_NORMAL;
            //从对象池中拿一个顶部正常障碍物
            Barrier top = BarrierPooL.get();
            top.setAttribute(Constant.Frame_Width,0,topHeight,type,true); //障碍物属性


            //从对象池中拿一个底部正常障碍物
            Barrier bottom = BarrierPooL.get();
            type = Barrier.TYPE_BOTTOM_NORMAL;
            bottom.setAttribute(Constant.Frame_Width,topHeight + TYPE1_VER_INTERVAL,Constant.Frame_Height - topHeight - TYPE1_HOR_INTERVAL,type,true);

            barries.add(top);
            barries.add(bottom);
        }else {
            //判断最后一个是否已经完全在游戏窗口中
            Barrier last = barries.get(barries.size() - 1);
            if (last.isInFrame()){
                if(GameTime.getInstance().getSecondTime() < GameTime.HOVER_Barrier_TIME){
                    addNormalBarrier(last);
                }else{
                    try {
                        if(GameUtil.isInAnyProbability(1,4)){
                            addHoverBarrier(last);
                        }else {
                            addNormalBarrier(last);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 添加普通障碍物
     * @param last
     */
    private void addNormalBarrier(Barrier last){
        int topHeight = GameUtil.getRandomNumber(TYPE1_MIN_HEIGHT,TYPE1_MAX_HEIGHT+1);
        int type = Barrier.TYPE_TOP_NORMAL;
        int x = last.getX() + TYPE1_HOR_INTERVAL;
        //从对象池中拿一个顶部正常障碍物
        Barrier top = BarrierPooL.get();
        top.setAttribute(x,0,topHeight,type,true);

        //从对象池中拿一个底部正常障碍物
        Barrier bottom = BarrierPooL.get();
        type = Barrier.TYPE_BOTTOM_NORMAL;
        bottom.setAttribute(x,topHeight + TYPE1_VER_INTERVAL,Constant.Frame_Height - topHeight - TYPE1_VER_INTERVAL,type,true);

        barries.add(top);
        barries.add(bottom);
    }
    /**
     *添加悬浮障碍物
     */
    private void addHoverBarrier(Barrier last){
        int Height = GameUtil.getRandomNumber(Constant.Frame_Height>>2,Constant.Frame_Height/3);
        int type = Barrier.TYPE_HOVER_NORMAL;
        int x = last.getX() + TYPE1_HOR_INTERVAL;
        int y = GameUtil.getRandomNumber(Constant.Frame_Height/3+100,Constant.Frame_Height/3>>2);
        Barrier hover = BarrierPooL.get();
        hover.setAttribute(x,y,Height,type,true);
        barries.add(hover);
    }
    /**
     * 判断鸟和障碍物的碰撞
     * @return
     */
    public boolean BirdCrush(Bird bird){
        //鸟死亡后不再添加障碍物
        if (bird.isDie()) return false;
        for (int i = 0; i < barries.size(); i++) {
            Barrier barrier = barries.get(i);
            if (barrier.getRect().intersects(bird.getRect())){  //相撞
                bird.dying();
                return true;
            }
        }
        return false;
    }

    public void reset(){
        for (int i = 0; i < barries.size(); i++) {
            Barrier barrier = barries.get(i);
            BarrierPooL.giveBack(barrier);
        }
        barries.clear();
    }
}
